﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace rock.DAL
{
	/// <summary>
	/// 数据访问类:DalUSER_ACCOUNT
	/// </summary>
	public partial class DalUSER_ACCOUNT
	{
		public DalUSER_ACCOUNT()
		{}
		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string ACCOUNT)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from USER_ACCOUNT");
			strSql.Append(" where ACCOUNT=@ACCOUNT ");
			SqlParameter[] parameters = {
					new SqlParameter("@ACCOUNT", SqlDbType.VarChar,30)			};
			parameters[0].Value = ACCOUNT;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
        public SqlParamInfo Add(rock.Model.ModelUSER_ACCOUNT model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into USER_ACCOUNT(");
			strSql.Append("ACCOUNT,PASSWORD,USERNAME,PHONE,NOTE)");
			strSql.Append(" values (");
			strSql.Append("@ACCOUNT,@PASSWORD,@USERNAME,@PHONE,@NOTE)");
			SqlParameter[] parameters = {
					new SqlParameter("@ACCOUNT", SqlDbType.VarChar,30),
					new SqlParameter("@PASSWORD", SqlDbType.VarChar,30),
					new SqlParameter("@USERNAME", SqlDbType.VarChar,50),
					new SqlParameter("@PHONE", SqlDbType.VarChar,100),
					new SqlParameter("@NOTE", SqlDbType.VarChar,300)};
			parameters[0].Value = model.ACCOUNT;
			parameters[1].Value = model.PASSWORD;
			parameters[2].Value = model.USERNAME;
			parameters[3].Value = model.PHONE;
			parameters[4].Value = model.NOTE;


            return new SqlParamInfo() { sql = strSql.ToString(), parameters = parameters };
            //int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
            //if (rows > 0)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public SqlParamInfo Update(rock.Model.ModelUSER_ACCOUNT model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update USER_ACCOUNT set ");
			strSql.Append("PASSWORD=@PASSWORD,");
			strSql.Append("USERNAME=@USERNAME,");
			strSql.Append("PHONE=@PHONE,");
			strSql.Append("NOTE=@NOTE");
			strSql.Append(" where ACCOUNT=@ACCOUNT ");
			SqlParameter[] parameters = {
					new SqlParameter("@PASSWORD", SqlDbType.VarChar,30),
					new SqlParameter("@USERNAME", SqlDbType.VarChar,50),
					new SqlParameter("@PHONE", SqlDbType.VarChar,100),
					new SqlParameter("@NOTE", SqlDbType.VarChar,300),
					new SqlParameter("@ACCOUNT", SqlDbType.VarChar,30)};
			parameters[0].Value = model.PASSWORD;
			parameters[1].Value = model.USERNAME;
			parameters[2].Value = model.PHONE;
			parameters[3].Value = model.NOTE;
			parameters[4].Value = model.ACCOUNT;

            return new SqlParamInfo() { sql = strSql.ToString(), parameters = parameters };
            //int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
            //if (rows > 0)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
		}

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public Boolean UpdatePwd(String account , String password , String newpassword)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update USER_ACCOUNT set ");
            strSql.Append("PASSWORD=@PASSWORD");
            strSql.Append(" where ACCOUNT=@ACCOUNT AND PASSWORD  =  @PASSWORD2");
            SqlParameter[] parameters = {
					new SqlParameter("@PASSWORD", SqlDbType.VarChar,30),
				    new SqlParameter("@ACCOUNT", SqlDbType.VarChar,30),
					new SqlParameter("@PASSWORD2", SqlDbType.VarChar,30)};
            parameters[0].Value = newpassword;
            parameters[1].Value = account;
             parameters[2].Value = password;

            
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public SqlParamInfo Delete(string ACCOUNT)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from USER_ACCOUNT ");
			strSql.Append(" where ACCOUNT=@ACCOUNT ");
			SqlParameter[] parameters = {
					new SqlParameter("@ACCOUNT", SqlDbType.VarChar,30)			};
			parameters[0].Value = ACCOUNT;


            return new SqlParamInfo() { sql = strSql.ToString(), parameters = parameters };
            //int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
            //if (rows > 0)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ACCOUNTlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from USER_ACCOUNT ");
			strSql.Append(" where ACCOUNT in ("+ACCOUNTlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public rock.Model.ModelUSER_ACCOUNT GetModel(string ACCOUNT)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ACCOUNT,PASSWORD,USERNAME,PHONE,NOTE from USER_ACCOUNT ");
			strSql.Append(" where ACCOUNT=@ACCOUNT ");
			SqlParameter[] parameters = {
					new SqlParameter("@ACCOUNT", SqlDbType.VarChar,30)			};
			parameters[0].Value = ACCOUNT;

			rock.Model.ModelUSER_ACCOUNT model=new rock.Model.ModelUSER_ACCOUNT();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ACCOUNT"]!=null && ds.Tables[0].Rows[0]["ACCOUNT"].ToString()!="")
				{
					model.ACCOUNT=ds.Tables[0].Rows[0]["ACCOUNT"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PASSWORD"]!=null && ds.Tables[0].Rows[0]["PASSWORD"].ToString()!="")
				{
					model.PASSWORD=ds.Tables[0].Rows[0]["PASSWORD"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USERNAME"]!=null && ds.Tables[0].Rows[0]["USERNAME"].ToString()!="")
				{
					model.USERNAME=ds.Tables[0].Rows[0]["USERNAME"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PHONE"]!=null && ds.Tables[0].Rows[0]["PHONE"].ToString()!="")
				{
					model.PHONE=ds.Tables[0].Rows[0]["PHONE"].ToString();
				}
				if(ds.Tables[0].Rows[0]["NOTE"]!=null && ds.Tables[0].Rows[0]["NOTE"].ToString()!="")
				{
					model.NOTE=ds.Tables[0].Rows[0]["NOTE"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetListForLogin(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select ACCOUNT,PASSWORD,USERNAME +'('+ACCOUNT+')' AS USERNAME,PHONE,NOTE  ");
            strSql.Append(" FROM USER_ACCOUNT ");
            if(strWhere.Trim()!="")
            {
                strSql.Append(" where "+strWhere);
            }

            return DbHelperSQL.QueryForOneAccess(strSql.ToString());
		}
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("select ACCOUNT,PASSWORD,USERNAME,PHONE,NOTE ");
            //strSql.Append(" FROM USER_ACCOUNT ");
            //if(strWhere.Trim()!="")
            //{
            //    strSql.Append(" where "+strWhere);
            //}
            strSql.Append(@"SELECT 
                            USER_ACCOUNT.ACCOUNT AS ACCOUNT,PASSWORD,USERNAME,PHONE,NOTE,
                            ISNULL(BASE_AUTHO,0) as BASE_AUTHO ,
                            ISNULL(REPORT_AUTHO,0) as REPORT_AUTHO,
                            ISNULL(HOUSE_AUTHO,0) as HOUSE_AUTHO,
                            ISNULL(CONFIG_AUTHO,0) as CONFIG_AUTHO,
                            ISNULL(MESSAGE_AUTHO,0) as MESSAGE_AUTHO,
                            ISNULL(PICTURE_AUTHO,0) as PICTURE_AUTHO,
                            ISNULL(FEE_AUTHO,0) as FEE_AUTHO
                            FROM USER_ACCOUNT   LEFT JOIN USER_AUTHO   ON  USER_ACCOUNT.ACCOUNT = USER_AUTHO.ACCOUNT  ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE  " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ACCOUNT,PASSWORD,USERNAME,PHONE,NOTE ");
			strSql.Append(" FROM USER_ACCOUNT ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM USER_ACCOUNT ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ACCOUNT desc");
			}
			strSql.Append(")AS Row, T.*  from USER_ACCOUNT T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "USER_ACCOUNT";
			parameters[1].Value = "ACCOUNT";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

